﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public class AnimatorResetBehaviour : StateMachineBehaviour
    {
        [Tooltip("The reset triggers when enter this state.")]
        [SerializeField]
        private string[] enterResets;

        [Tooltip("The reset triggers when leave this state.")]
        [SerializeField]
        private string[] exitResets;

        private int[] enterhash, exithash;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (this.enterResets != null)
            {
                this.enterhash = new int[this.enterResets.Length];
                for (int i = 0; i < this.enterhash.Length; i++)
                {
                    this.enterhash[i] = Animator.StringToHash(this.enterResets[i]);
                }
                this.enterResets = null;
            }
            if (this.enterhash == null) return;

            for(int i=0,imax = this.enterhash.Length; i < imax; i++)
            {
                animator.ResetTrigger(this.enterhash[i]);
            }

        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (this.exitResets != null)
            {
                this.exithash = new int[this.exitResets.Length];
                for (int i = 0; i < this.enterhash.Length; i++)
                {
                    this.exithash[i] = Animator.StringToHash(this.exitResets[i]);
                }
                this.exitResets = null;
            }
            if (this.exithash == null) return;

            for (int i = 0, imax = this.exithash.Length; i < imax; i++)
            {
                animator.ResetTrigger(this.exithash[i]);
            }
        }

    }

}

